﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
namespace Group16.Class_Library
{
    public class Customer
    {
        private string _name;
        private string _address;
        private string _code;
        private string _phone;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        public string Code
        {
            get
            {
                if (String.IsNullOrEmpty(_code))
                    MessageBox.Show("Code is empty");
                return _code;
            }
            set {
                if (String.IsNullOrEmpty(value))
                    MessageBox.Show("Code is empty");
                _code = value; 
            }
        }
        public string Address
        {
            get { return _address; }
            set { _address = value; }
        }
        public string Phone
        {
            get { return _phone; }
            set { _phone = value; }
        }
        public void LogData()
        {
            string comText = "insert into Customer values('"+Code+"','"+Name+"','"+Address+"','"+Phone+"')";
            try
            {
                DataBaseInteraction.ExecuteCommand(comText, 1);
            }
            catch (SqlException se)
            {
                MessageBox.Show("Unable to log customer's infomation  to database " + se.Message);
            }
            finally
            {
                DataBaseInteraction.Connection.Close();
            }
        }
        public void ReadDataBase(string ivCode)
        {
            string comText = "Select distinct Code, Name,Customer.Address, PhoneNumber from InvoiceDetails inner join Customer "
                                + "On InvoiceDetails.CustomerCode = Customer.Code "
                                + "where InvoiceDetails.InvoiceCode = '" + ivCode + "'";
            try
            {
                using (SqlDataReader reader = (SqlDataReader) DataBaseInteraction.ExecuteCommand(comText, 2))
                {
                    reader.Read();
                    Code = reader.GetString(0);
                    Name = reader.GetString(1);
                    Address = reader.GetString(2);
                    Phone = reader.GetString(3);
                }
            }
            catch (SqlException se)
            {
                MessageBox.Show("Unable to read data for customer infomation" + se.Message);
            }
            finally
            {
                DataBaseInteraction.Connection.Close();
            }
        }
    }
}
